<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 基础表格
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.task_id" placeholder="task_id" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-delete" @click="handleBatchDelete">批量删除</el-button>
            </div>
            <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header" @selection-change="selectionChange">
                <el-table-column type="selection"></el-table-column>
                <el-table-column prop="task_id" label="task_id" width="55" align="center" show-overflow-tooltip></el-table-column>
                <el-table-column prop="content" label="content" width="55" align="center" show-overflow-tooltip></el-table-column>
                <el-table-column prop="startTime" label="开始时间" show-overflow-tooltip></el-table-column>
                <el-table-column prop="endTime" label="结束时间" show-overflow-tooltip></el-table-column>
                <el-table-column prop="open_id_receiver" label="发布者id" show-overflow-tooltip></el-table-column>
                <el-table-column prop="open_id_builder" label="接收者id" show-overflow-tooltip></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template #default="scope">
                        <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑
                        </el-button>
                        <el-button type="text" icon="el-icon-delete" class="red"
                            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination background layout="total, prev, pager, next" :current-page="query.pageIndex"
                    :page-size="query.pageSize" :total="pageTotal" @current-change="handlePageChange"></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" v-model="editVisible" width="80%">
            <el-form label-width="200px;">
                <el-form-item label="task_id">
                    <el-input v-model="form.task_id" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="content">
                    <el-input v-model="form.content"></el-input>
                </el-form-item>
                <el-form-item label="startTime">
                    <el-input v-model="form.startTime" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="endTime">
                    <el-input v-model="form.endTime" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="open_id_builder">
                    <el-input v-model="form.open_id_builder" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="open_id_receiver">
                    <el-input v-model="form.open_id_receiver" :disabled="true"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="editVisible = false">取 消</el-button>
                    <el-button type="primary" @click="saveEdit">确 定</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script>
import { ref, reactive } from "vue";
import { ElImage, ElMessage, ElMessageBox } from "element-plus";
import { getTaskList } from "../api/task/index";
import {getTask} from "../api/task/index";
import { batchDeleteTask } from "../api/task/index";
import { updateTask } from "../api/task/index";

export default {
    name: "basetable",
    setup() {
        const query = reactive({
            address: "",
            name: "",
            pageIndex: 1,
            pageSize: 5,
            taskIds: []
        });
        const tableData = ref([]);
        const pageTotal = ref(0);
        // 获取表格数据
        const getData = () => {
            getTaskList(query).then((res) => {
                tableData.value = res.list;
                pageTotal.value = res.length
                // pageTotal.value = res.pageTotal || 50;
            });
        };
        getData();

        // 查询操作
        const handleSearch = () => {
            // query.pageIndex = 1;
            if(Boolean(query.task_id)) {

                getTask(query).then((res) => {
                    tableData.value = res.list
                })
            }else {
                getData();
            }
            
        };
        // 选中项改变
        const selectionChange = (selection ,row) => {
            let array = [];
            selection.forEach((element) => {
                array.push(element.task_id)
            })
            query.taskIds = array
        };
        // 分页导航
        const handlePageChange = (val) => {
            query.pageIndex = val;
            getData();
        };
        // 批量删除
        const handleBatchDelete = () => {
            if(JSON.stringify(query.taskIds) != "[]") {
                ElMessageBox.confirm("确定要批量删除吗？", "提示", {
                    type: "warning",
                    }).then(() => {
                        // let query = tableData._rawValue[index];
                        batchDeleteTask(query.taskIds).then((res) => {
                            console.log(res, 'res')
                            if(Number(res) == 200) {
                                ElMessage.success("删除成功");
                                getData()
                            }
                        })
                })
                .catch(() => {
                    ElMessage.error("删除失败");
                });
            }else {
            }
        }
        // 删除操作
        const handleDelete = (index, row) => {
            // 二次确认删除
            ElMessageBox.confirm("确定要删除吗？", "提示", {
                type: "warning",
            })
                .then(() => {
                    ElMessage.success("删除成功");
                    tableData.value.splice(index, 1);
                })
                .catch(() => {});
        };

        // 表格编辑时弹窗和保存
        const editVisible = ref(false);
        let form = reactive({
            task_id: 0,
            content: "",
            startTime: "",
            endTime: "",
            open_id_builder: "",
            open_id_receiver: ""
        });
        let idx = -1;
        const handleEdit = (index, row) => {
            idx = index;
            Object.keys(form).forEach((item) => {
                form[item] = row[item];
            });
            editVisible.value = true;
        };
        const saveEdit = () => {
            editVisible.value = false;
            updateTask(form).then((res) => {
                if(Number(res) == 200) {
                    ElMessage.success(`修改第 ${idx + 1} 行成功`);
                    Object.keys(form).forEach((item) => {
                        tableData.value[idx][item] = form[item];
                    });
                }
            })
        };

        return {
            query,
            tableData,
            pageTotal,
            editVisible,
            form,
            handleSearch,
            handlePageChange,
            handleDelete,
            handleBatchDelete,
            selectionChange,
            handleEdit,
            saveEdit,
        };
    },
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
